package com.xxx.controller;

import com.xxx.service.MessageProducer;
import com.xxx.service.TransactionalProducerService;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    MessageProducer messageProducer;

//    @Autowired
//    TxMessageProducer txMessageProducer;

    @Resource
    TransactionalProducerService transactionalProducerService;

    /**
     * 测试发送rocketmq消息
     * http://localhost:8080/test/sendMsg?msg=111111111aaaaaaa
     */
    @RequestMapping("/sendMsg")
    public String sendMsg(String msg) throws MQBrokerException, RemotingException, InterruptedException, MQClientException {
        messageProducer.sendMessage("test-topic", msg);
        return "send message 【"+msg+"】 success!";
    }

    /**
     * 测试发送延时消息
     * http://localhost:8080/test/sendDelayMsg?msg=delayMsg1
     */
    @GetMapping("/sendDelayMsg")
    public String sendDelayMsg(String msg) throws MQBrokerException, RemotingException, InterruptedException, MQClientException {
        String delayMsg = "{'delay msg': '"+msg+"'}";
        messageProducer.sendDelayMessage("delay-topic", delayMsg);
        return "send delay message 【"+msg+"】 success!";
    }

//    /**
//     * 测试发送事务消息④
//     * http://localhost:8080/test/sendTxMsg?msg=txMsg1
//     */
//    @GetMapping("/sendTxMsg")
//    public String sendTxMsg(String msg) throws MQClientException {
//        String txMsg = "{'tx msg': '"+msg+"'}";
//        txMessageProducer.sendTxMessage("tx-topic", txMsg);
//        return "send tx message 【"+msg+"】 success!";
//    }

    /**
     * 测试rocketmq原生事务消息②
     * http://localhost:8080/test/sendTxMessage?orderId=ORDER_001
     */
    @GetMapping("/sendTxMessage")
    public String sendTxMessage(@RequestParam String orderId) throws Exception {
        String txMsg = "{'orderNo': '"+orderId+"', 'orderMoney': '100.00'}";
        transactionalProducerService.sendTransactionalMessage("tx-topic", txMsg);
        return "事务消息已发送，订单ID：" + orderId;
    }

    /**
     * 测试发送顺序消息
     * http://localhost:8080/test/sendOrderedMsg?orderId=ORDER_001
     */
    @GetMapping("/sendOrderedMsg")
    public String sendOrderedMsg(String orderId) throws MQBrokerException, RemotingException, InterruptedException, MQClientException {
        String orderedMsg = "{'orderNo': '"+orderId+"', 'orderMoney': '200.00'}";
        messageProducer.sendOrderedMessage("ordered-topic", orderedMsg, orderId);
        return "send ordered message 【"+orderedMsg+"】 success!";
    }

    /**
     * 测试批量发送顺序消息
     * http://localhost:8080/test/batchSendOrderedMsg?orderId=ORDER_001
     */
    @GetMapping("/batchSendOrderedMsg")
    public String batchSendOrderedMsg(String orderId) throws MQBrokerException, RemotingException, InterruptedException, MQClientException {
        for (int i = 1; i <= 10; i++) {
            String orderedMsg = "{'orderNo': '"+orderId+"', 'orderMoney': '"+i+"00.00'}";
            messageProducer.sendOrderedMessage("ordered-topic", orderedMsg, orderId);
        }
        return "batch send ordered message 【"+orderId+"】 success!";
    }

}
